import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '../views/Login.vue'
import HomePage from '../views/Home.vue'

// 路由守卫：未登录禁止访问首页
const checkAuth = (to, from, next) => {
    const token = localStorage.getItem('token')
    if (to.path !== '/login' && !token) {
        next('/login')
    } else {
        next()
    }
}

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: LoginPage },
    {
        path: '/home',
        component: HomePage,
        beforeEnter: checkAuth
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

export default router